package com.controller;

import com.Service.MedicalCardService;
import com.model.pojo.Buildcard;
import com.model.pojo.MedicalCard;
import com.util.GetNumber;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("Initialize")
public class MedicalCardController {
    @Resource
    private MedicalCardService medicalCardService;
    private MedicalCard imedicalCard;

    @RequestMapping("Initialize")
    public String Initialize() {

        return "MedicalCard";
    }

    @RequestMapping("Initializecard")
    public String Initializecard(int initialnum, int endnum) {
        System.out.println(initialnum);
        System.out.println(endnum);
        if (endnum == 0) {
            String cardnum = GetNumber.GetSerialNumber(initialnum);
            int x = medicalCardService.set(cardnum);
        } else {
            for (int i = initialnum; i <= endnum; i++) {
                System.out.println(i);
                String cardnum = GetNumber.GetSerialNumber(i);
                int x = medicalCardService.set(cardnum);
            }
        }

        return "MedicalCard";
    }

    @RequestMapping("select")
    public String select(MedicalCard medicalCard,HttpSession session) {
        List<MedicalCard> list = medicalCardService.selectall(medicalCard);
        session.setAttribute("list", list);
        return "MedicalCardList";
    }

    @RequestMapping("selectcard")
    public String selectcard(MedicalCard medicalCard, HttpSession session) {
        List<MedicalCard> list = medicalCardService.selectall(medicalCard);
        session.setAttribute("list", list);
        return "MedicalCardList";
    }

    @RequestMapping("/getcardnum")
    @ResponseBody
    public String getcardnum() {

        List<MedicalCard> list = medicalCardService.getcardnum();
        imedicalCard =  list.get(0);
        String data = imedicalCard.getMedicalCardNumber();
        return data;
    }

    @RequestMapping("reset")
    public String reset(MedicalCard medicalCard) {
        System.out.println(medicalCard.getMedicalCardNumber());
        int i=medicalCardService.reset(medicalCard);

        System.out.println(i);
        return "redirect:select";
    }

}
